<?php
/**
 * Created by PhpStorm.
 * User: 快定
 * Date: 2021/7/9
 * Time: 22:04
 */

class AuthController extends AppController
{
    public $layout = null;
    public $components = array('publicFunction','Excle');
    /*
    * 显示权限列表
    */
    public function index(){
        $request = $_SERVER['REQUEST_METHOD'] == "POST" ?$_POST :$_GET;
        $reqData= $this->publicFunction->filterParams($request);
        $reqData['id'] = $reqData['id'] ? $reqData['id'] : 0;
        if ($reqData['id']){
            $where["OR"] = [
                ["Auth.pid"=>$reqData['id']],
                ['Auth.id'=>$reqData['id']]
            ];
        }
        //获取数据
        $data = $this->Auth->getAuth($where);

        $rootMenu = $this->Auth->getAuth(array('is_menu'=>1));
        //主菜单
        $root = $this->Auth->getAuth(array('pid'=>0));
        $list = [];
        $list1 = [];
        foreach ($data as $v) {
            $list[] = $v['Auth'];
        }
        foreach ($rootMenu as $v1) {
            $list1[] = $v1['Auth'];
        }
        //父子级树状结构
        $data = $this->publicFunction->get_tree_list($list);
        $rootMenu = $this->publicFunction->get_tree_list($list1);
        $this->set(array('data'=>$data ,'rootMenu'=>$rootMenu,'root'=>$root,'id'=>$reqData['id']));
    }

    /*
     * 修改权限
     */
    public function editAuth()
    {
        //接受参数
        $post = $_POST;
        $data = $this->Auth->dellAuth($post);
        //参数验证
        if ($data) {
            //数据操作
            $this->publicFunction->success($data, '操作权限成功');
        }
        $this->publicFunction->fail(400, '操作权限失败');
    }

    /*
     * 删除权限
     */
    public function delAuth()
    {
        $id = $_POST['id'];
        if (empty($id)) {
            $this->publicFunction->fail(400, '参数错误');
        }
        $res = $this->Auth->del($id);
        if ($res) {
            $this->publicFunction->success($res, '删除权限成功');
        }
        $this->publicFunction->fail(400, '删除权限失败');
    }
}